package com.blueocean_health.care.management.service.impl;

import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.blueocean_health.care.management.common.LocalManager;
import com.blueocean_health.care.management.common.base.string.StringUtils;
import com.blueocean_health.care.management.domain.base.SysResult;
import com.blueocean_health.care.management.domain.base.SysResultByPage;
import com.blueocean_health.care.management.domain.base.User;
import com.blueocean_health.care.management.domain.dto.PullDownDto;
import com.blueocean_health.care.management.domain.dto.SelectNurseGroupDto;
import com.blueocean_health.care.management.domain.groupvo.SelectNurseGroupVo;
import com.blueocean_health.care.management.em.LocalManagerType;
import com.blueocean_health.care.management.em.NurseFeeTypeEnum;
import com.blueocean_health.care.management.mapper.NurseGroupMapper;
import com.blueocean_health.care.management.service.SelectNurseGroupService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;

/**
* 小组模块查询service实现类 
* @author huyanqiu
* @date 2018年1月4日 上午9:54:15
* @version 版本号：1.0
*/
@Service
public class SelectNurseGroupServiceImpl implements SelectNurseGroupService{

	@Autowired
	private NurseGroupMapper nurseGroupMapper;
	
	/**
	 * 查询小组详细信息
	 */
	@Override
	public SysResult query(SelectNurseGroupDto selectNurseGroupDto) {
		// 获得当前登录用户信息
    	User user = LocalManager.getVal(LocalManagerType.USER_WEB);
		SelectNurseGroupVo selectNurseGroupVo = nurseGroupMapper.query(selectNurseGroupDto, user.getHospitalId());
		selectNurseGroupVo.setNurseFeeTypeName(NurseFeeTypeEnum.valueOf("_"+selectNurseGroupVo.getNurseFeeType()).getVal());
		if (StringUtils.isEmpty(selectNurseGroupVo)) {
			return SysResult.error("查无记录");
		}
		return SysResult.ok(selectNurseGroupVo);
	}

	/**
	 * 分页查询小组列表
	 */
	@SuppressWarnings({ "unchecked", "rawtypes" })
	@Override
	public SysResult list(SelectNurseGroupDto selectNurseGroupDto) {
		// 获得当前登录用户信息
    	User user = LocalManager.getVal(LocalManagerType.USER_WEB);
    	// 设置分页
    	PageHelper.startPage(selectNurseGroupDto.getPage(), selectNurseGroupDto.getPageSize());
    	// 查询小组列表
		List<SelectNurseGroupVo> selectNurseGroupVoList= nurseGroupMapper.list(selectNurseGroupDto, user.getHospitalId());
		for (SelectNurseGroupVo selectNurseGroupVo : selectNurseGroupVoList) {
			if(!StringUtils.isEmpty(selectNurseGroupVo.getNurseFeeType())){
				selectNurseGroupVo.setNurseFeeTypeName(NurseFeeTypeEnum.valueOf("_"+selectNurseGroupVo.getNurseFeeType()).getVal());
			}else{
				selectNurseGroupVo.setNurseFeeTypeName(null);
			}
		}
		PageInfo<SelectNurseGroupVo> pageInfo = new PageInfo(selectNurseGroupVoList);
		return SysResultByPage.ok(0, "SUCCESS", pageInfo.getList(), pageInfo.getTotal());
	}

	/**
	 * 导出列表-查询所有小组
	 */
	@Override
	public List<SelectNurseGroupVo> listExcel(SelectNurseGroupDto selectNurseGroupDto) {
		// 获得当前登录用户信息
    	User user = LocalManager.getVal(LocalManagerType.USER_WEB);
    	// 查询小组列表
    	List<SelectNurseGroupVo> selectNurseGroupVoList= nurseGroupMapper.list(selectNurseGroupDto, user.getHospitalId());
    	for (SelectNurseGroupVo selectNurseGroupVo : selectNurseGroupVoList) {
    		if(!StringUtils.isEmpty(selectNurseGroupVo.getNurseFeeType())){
    			selectNurseGroupVo.setNurseFeeTypeName(NurseFeeTypeEnum.valueOf("_"+selectNurseGroupVo.getNurseFeeType()).getVal());
    			
    		}
		}
		return selectNurseGroupVoList;
	}

	/**
	 * 新建类型为多对多订单，模糊查询小组
	 */
	@Override
	public SysResult selectGroup(SelectNurseGroupDto selectNurseGroupDto) {
		// 获得当前登录用户信息
    	User user = LocalManager.getVal(LocalManagerType.USER_WEB);
    	List<Map<String, String>> list = nurseGroupMapper.selectGroup(selectNurseGroupDto, user.getHospitalId());
		return SysResult.ok(list);
	}

	/**
	 * 选择成员
	 */
	@Override
	public SysResult selectNurse(SelectNurseGroupDto selectNurseGroupDto) {
		// 获得当前登录用户信息
    	User user = LocalManager.getVal(LocalManagerType.USER_WEB);
    	List<Map<String, String>> list = nurseGroupMapper.selectNurse(selectNurseGroupDto, user.getHospitalId());
    	return SysResult.ok(list);
	}

	@Override
	public List<PullDownDto> getGroupPull() {
		User user = LocalManager.getVal(LocalManagerType.USER_WEB);
		List<PullDownDto> groupPull = nurseGroupMapper.getGroupPull(user.getHospitalId());
		return groupPull;
	}

}
